//
//  BusinessProductController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/10/27.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "BusinessProductController.h"
#import "homeCollectionViewCell.h"
#import "GoodListsModel.h"
#import "ShopDetailsController.h"
#import "ProductSearchViewController.h"

@interface BusinessProductController ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
{
    TopPullRefreshAnaimation *gifHeader;
    MBProgressHUD *progressHUD;
    NSUInteger refreshIndex;
    NSDictionary *_urlDict;
}

@property(nonatomic,strong)UICollectionView *businessProductView;
@property(nonatomic,strong)NSMutableArray *businessProductArray;

@end

@implementation BusinessProductController


static NSString * const BusinessProductReuseIdentifier = @"BusinessProductReuseIdentifier";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(businessProductSearch)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:15],NSFontAttributeName, nil] forState:UIControlStateNormal];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
    
    [self addCollectionView];
    //上啦刷新
    [self addPullUpdateData];
    
    [self getBusinessProductListsRequestData];
    
    refreshIndex = 1;
}

#pragma mark 商家产品搜索
- (void)businessProductSearch
{
    ProductSearchViewController *ProductSearchVc = [[ProductSearchViewController alloc] init];
    ProductSearchVc.businessId = _businessId;
    ProductSearchVc.isBussinessProduct = true;
    [self.navigationController pushForm:self.navigationController pushToAndHidden:ProductSearchVc];
}


#pragma mark 上啦刷新
- (void)addPullUpdateData
{
    self.businessProductView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(getBusinessProductListsLoadMoreData)];
    
}

- (void)getBusinessProductListsLoadMoreData
{
    refreshIndex ++;
    [self getBusinessProductListsRequestData];
    [self.businessProductView.mj_footer endRefreshing];
}


#pragma mark >>>>>>>>>获取商品列表请求
- (void)getBusinessProductListsRequestData
{
    //菊花加载
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
    if (refreshIndex == 1) {
        [self.businessProductArray removeAllObjects];
    }
    NSString *P = [NSString stringWithFormat:@"%ld",refreshIndex];
    
    NSDictionary *params = @{@"shopId":[NSString stringWithFormat:@"%@", _businessId],
                             @"p":P
                             };
    
    NSLog(@"%@",params);
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Business/shopBusinessList" completeBlock:^(id object) {
        
        NSLog(@"---------------------%@",object);
        if (JudgeBackDataCode) {
            
            NSDictionary *tempDict = object[@"data"];
            NSArray *arr = [tempDict objectForKey:@"list"];
            _urlDict = tempDict[@"picprex"];
            
            NSLog(@"%@",_urlDict);
            for (NSDictionary *dict in arr) {
                GoodListsModel *goodListsModel = [[GoodListsModel alloc] initWithDictionary:dict error:nil];
                if (goodListsModel) {
                    [self.businessProductArray addObject:goodListsModel];
                    
                }
            }
            
        }else{
            //没有更多数据
            [self.businessProductView.mj_footer endRefreshingWithNoMoreData];
        }
        //菊花隐藏
        [progressHUD hide:YES];
        [self.businessProductView reloadData];
        
    }];
    
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.businessProductArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    homeCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:BusinessProductReuseIdentifier forIndexPath:indexPath];
    
    GoodListsModel *goodListsModel = self.businessProductArray[indexPath.item];
    
    [cell.productImage sd_setImageWithURL:[NSURL URLWithString:goodListsModel.picurl] placeholderImage:[UIImage imageNamed:@"goodsLoadJGG.jpg"]];
    cell.productImage.contentMode = UIViewContentModeScaleToFill;
    
    cell.productName.text = goodListsModel.title;
    cell.price.text = [NSString stringWithFormat:@"专享价: %@",goodListsModel.huiyuanjia];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
    
}

#pragma mark >>>>>>>>collectionView 代理
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodListsModel *goodListsModel = self.businessProductArray[indexPath.item];
    ShopDetailsController *detailsVC = [[ShopDetailsController alloc] init];
    detailsVC.goodsId = goodListsModel.replaceID;
    [self.navigationController pushForm:self.navigationController pushToAndHidden:detailsVC];
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake(WIDTH/2-2, HEIGHT/2.8);
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(1,1,1,1);
}


#pragma mark UI
- (void)addCollectionView
{
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.minimumLineSpacing = 2;
    flowlayout.minimumInteritemSpacing = 0;
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView *businessProductView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,WIDTH, HEIGHT-64) collectionViewLayout:flowlayout];
    self.businessProductView = businessProductView;
    self.businessProductView.delegate = self;
    self.businessProductView.dataSource = self;
    businessProductView.backgroundColor = BACKGROUND_COLOR;
    [self.view addSubview:businessProductView];
    
    [self.businessProductView registerClass:[homeCollectionViewCell class] forCellWithReuseIdentifier:BusinessProductReuseIdentifier];
    
}


- (NSMutableArray *)businessProductArray {
    if (!_businessProductArray) {
        _businessProductArray = [NSMutableArray array];
    }
    return _businessProductArray;
}


@end
